package chapt04.atom2;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.awt.GLCanvas;

import com.jogamp.opengl.util.FPSAnimator;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Atom2AWT
{
    static
    {
        // setting this true causes window events not to get sent on Linux if you run from inside Eclipse
        GLProfile.initSingleton(false);
    }

    public static void main(String [] args)
    {
        GLProfile glprofile = GLProfile.getDefault();
        GLCapabilities glcapabilities = new GLCapabilities(glprofile);
        final GLCanvas glcanvas = new GLCanvas(glcapabilities);

        glcanvas.addGLEventListener(new GLEventListener() {
            
            @Override
            public void reshape(GLAutoDrawable glautodrawable, int x, int y, int w, int h)
            {
            	Atom2.setup(glautodrawable.getGL().getGL2(), w, h);
            }
            
            @Override
            public void init(GLAutoDrawable glautodrawable)
            {
            	FPSAnimator animator = new FPSAnimator(glautodrawable, 15);
				animator.start();
            }
            
            @Override
            public void dispose( GLAutoDrawable glautodrawable)
            {
            	
            }
            
            @Override
            public void display(GLAutoDrawable glautodrawable)
            {
				int w = glautodrawable.getWidth();
				int h = glautodrawable.getHeight();
				Atom2.render(glautodrawable.getGL().getGL2(), w, h);
            }
        });

        final Frame frame = new Frame("OpenGL Atom - Part Duex");
        frame.add(glcanvas);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowevent)
            {
                frame.remove(glcanvas);
                frame.dispose();
                System.exit(0);
            }
        });

        frame.setSize(600, 400);
        frame.setVisible(true);
    }
}
